﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace GoGrid
{
    public class Image : IEquatable<Image>
    {
        public Connection Connection { get; set; }

        public static Image CreateFromXmlNode(XmlNode objectElementNode, Connection connection)
        {
            Image output = new Image();
            output.Connection = connection;

            output.ID = InternalHelper.GetAttributeElementValueFromXmlNodeAsNullableInt32(objectElementNode, "id");
            output.Name = InternalHelper.GetAttributeElementValueFromXmlNodeAsString(objectElementNode, "name");
            output.FriendlyName = InternalHelper.GetAttributeElementValueFromXmlNodeAsString(objectElementNode, "friendlyName");
            output.Description = InternalHelper.GetAttributeElementValueFromXmlNodeAsString(objectElementNode, "description");
            output.Location = InternalHelper.GetAttributeElementValueFromXmlNodeAsString(objectElementNode, "location");
            output.IsActive = InternalHelper.GetAttributeElementValueFromXmlNodeAsNullableBoolean(objectElementNode, "isActive");
            output.IsPublic = InternalHelper.GetAttributeElementValueFromXmlNodeAsNullableBoolean(objectElementNode, "isPublic");
            output.CreatedTime = InternalHelper.GetAttributeElementValueFromXmlNodeAsNullableDateTimeFromUnixEpoch(objectElementNode, "createdTime");
            output.UpdatedTime = InternalHelper.GetAttributeElementValueFromXmlNodeAsNullableDateTimeFromUnixEpoch(objectElementNode, "updatedTime");
            output.Price = InternalHelper.GetAttributeElementValueFromXmlNodeAsDecimal(objectElementNode, "price");

            XmlNode operatingSystemAttributeXmlNode = InternalHelper.GetAttributeElementFromXmlNodeAsFirstChildXmlNode(objectElementNode, "os");
            output.OperatingSystem = Option.CreateFromXmlNode(operatingSystemAttributeXmlNode, connection);

            XmlNode architectureAttributeXmlNode = InternalHelper.GetAttributeElementFromXmlNodeAsFirstChildXmlNode(objectElementNode, "architecture");
            output.Architecture = Option.CreateFromXmlNode(architectureAttributeXmlNode, connection);

            XmlNode typeAttributeXmlNode = InternalHelper.GetAttributeElementFromXmlNodeAsFirstChildXmlNode(objectElementNode, "type");
            output.Type = Option.CreateFromXmlNode(typeAttributeXmlNode, connection);

            XmlNode stateAttributeXmlNode = InternalHelper.GetAttributeElementFromXmlNodeAsFirstChildXmlNode(objectElementNode, "state");
            output.State = Option.CreateFromXmlNode(stateAttributeXmlNode, connection);

            XmlNode ownerAttributeXmlNode = InternalHelper.GetAttributeElementFromXmlNodeAsFirstChildXmlNode(objectElementNode, "owner");
            output.Owner = Customer.CreateFromXmlNode(ownerAttributeXmlNode, connection);

            XmlNode billingTokensAttributeXmlNode = InternalHelper.GetAttributeElementFromXmlNodeAsXmlNode(objectElementNode, "billingtokens");
            XmlNodeList billingTokensXmlNodeList = billingTokensAttributeXmlNode.SelectNodes("list/object[@name = 'billingtoken']");

            List<BillingToken> temporaryBillingTokenList = new List<BillingToken>();
            foreach (XmlNode billingTokenXmlNode in billingTokensXmlNodeList)
            {
                BillingToken billingToken = BillingToken.CreateFromXmlNode(billingTokenXmlNode, connection);
                temporaryBillingTokenList.Add(billingToken);
            }
            output.BillingTokens = temporaryBillingTokenList.ToArray();

            return output;
        }

        public int? ID { get; set; }
        public string Name { get; set; }
        public string FriendlyName { get; set; }
        public string Description { get; set; }
        public string Location { get; set; }
        public bool? IsActive { get; set; }
        public bool? IsPublic { get; set; }
        public DateTime? CreatedTime { get; set; }
        public DateTime? UpdatedTime { get; set; }
        public Option OperatingSystem { get; set; }
        public Option Architecture { get; set; }
        public Customer Owner { get; set; }
        public Option Type { get; set; }
        public Option State { get; set; }
        public decimal Price { get; set; }
        public BillingToken[] BillingTokens { get; set; }

        public override int GetHashCode()
        {
            return this.ID ?? base.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            return this.Equals(obj as Image);
        }

        public bool Equals(Image other)
        {
            if (other == null) return false;
            return this.ID == other.ID;
        }
    }
}